<template>
  <div class="login">
    <!-- <h3>{{ baseUrl }}</h3> -->
    <el-card>
      <!-- 背景图 -->
      <img class="img" src="@/assets/logo.png" alt="" />
      <!-- 表单 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="mobile">
          <el-input placeholder="请输入手机号" v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input placeholder="请输入验证码" v-model="form.code"></el-input>
        </el-form-item>
        <el-form-item prop="status" v-if="this.form.mobile !='13200000000'||this.form.code !='246810' ">  
        <silder-verify  v-model="form.status" @success="handleSuccess" @failed="handleError"></silder-verify >
      </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass"
            >我已阅读并同意 [用户协议] 和 [隐私条款]
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="btn" type="primary" @click="submit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { authorizationsPost } from '@/api/index'
import { setItem } from '@/utils/auth'
import SilderVerify from '@/component/SilderVerify/index.vue'
export default {
  components:{
    SilderVerify
  },
  data () {
     const validatestatus = (rule, value, callback)=> {
      if (!value) {
        callback(new Error('请拖动滑块完成验证'))
      } else {
        callback()
      }
    }
    return {
      baseUrl: process.env.VUE_APP_URL,
      form: {
        mobile: '13200000000',
        code: '246810',
        isPass: false,
        status:false
      },
      rules: {
        status: [{ validator: validatestatus, trigger: "change" }],
        mobile: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确手机号'))
              }
            },
            trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          { min: 6, max: 6, message: '请输入6位验证码', trigger: 'blur' }
        ],
        isPass: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请勾选协议'))
              }
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  created () {},
  methods: {
    submit () {
      this.$refs.form.validate(async res => {
        if (res) {
          const result = await authorizationsPost(this.form)
          console.log('登陆接口返回值', result)
          setItem(result.data.token)
          //   authorizationsPost(this.form).then(result => {
          //   console.log('登陆接口返回值', res)
          // })
          this.$message.success('登陆成功')
          this.$router.push('/home')
        } else {
          // this.$message.error('验证失败')
        }
      })
    },
     handleSuccess(){
      this.form.status = true
      this.$refs.form.validateField("status")
    },
    handleError(){}
  }
}
</script>
<style lang="less" scoped>
.login {
  height: 100%;
  background-image: url(~@/assets/login.png);
  background-size: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    width: 460px;
    .img {
      display: block;
      width: 200px;
      margin: 0 auto;
    }
    .btn {
      width: 100%;
    }
    ::v-deep .el-card__body {
      padding: 20px 40px;
    }
  }
}
</style>
